package cn.hs.lyxf.web.controller;

import cn.hs.lyxf.domain.MaintenanceWork;
import cn.hs.lyxf.query.MaintenanceWorkQuery;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.result.PageList;
import cn.hs.lyxf.service.IMaintenanceWorkService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/maintenanceWork")
public class MaintenanceWorkController {

    @Autowired
    public IMaintenanceWorkService maintenanceWorkService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody MaintenanceWork maintenanceWork) {
        if (maintenanceWork.getId() != null) {
            maintenanceWorkService.updateById(maintenanceWork);
        } else {
            maintenanceWorkService.insert(maintenanceWork);
        }
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        maintenanceWorkService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(maintenanceWorkService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(maintenanceWorkService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody MaintenanceWorkQuery query) {
        Page<MaintenanceWork> page = new Page<MaintenanceWork>(query.getPage(), query.getRows());
        page = maintenanceWorkService.selectPage(page);
        return JsonResult.success(new PageList<MaintenanceWork>(page.getTotal(), page.getRecords()));
    }

    /**
     * 维保员进行维保签到
     */
    @RequestMapping(value = "/work/signIn", method = RequestMethod.GET)
    public JsonResult signIn(@RequestBody MaintenanceWork maintenanceWork) {
        return maintenanceWorkService.signIn(maintenanceWork);
    }

    /**
     * 维保人员上传问题记录
     */
    @RequestMapping(value = "/problemRecord", method = RequestMethod.GET)
    public JsonResult problemRecord(@RequestBody MaintenanceWork maintenanceWork) {
        return maintenanceWorkService.problemRecord(maintenanceWork);
    }

    /**
     * 维保完成
     */
    @RequestMapping(value = "/finish/{workId}", method = RequestMethod.GET)
    public JsonResult finish(@PathVariable("workId") Long workId) {
        return maintenanceWorkService.finish(workId);
    }
}
